package com.fumihiko.modules.jxxm.service.impl;

import com.fumihiko.modules.jxxm.entity.RetiredEmployeesEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fumihiko.common.utils.PageUtils;
import com.fumihiko.common.utils.Query;

import com.fumihiko.modules.jxxm.dao.HonorableEmployeesDao;
import com.fumihiko.modules.jxxm.entity.HonorableEmployeesEntity;
import com.fumihiko.modules.jxxm.service.HonorableEmployeesService;


@Service("honorableEmployeesService")
public class HonorableEmployeesServiceImpl extends ServiceImpl<HonorableEmployeesDao, HonorableEmployeesEntity> implements HonorableEmployeesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String myStatus = (String)params.get("myStatus");
        String name = (String)params.get("name");
        String idNo = (String)params.get("idNo");
        String personType = (String)params.get("personType");
        String originalTroops = (String)params.get("originalTroops");
        String retirementStatus = (String)params.get("retirementStatus");
        String duties = (String)params.get("duties");
        String county = (String)params.get("county");
        String street = (String)params.get("street");
        String birthDate1 = (String)params.get("birthDate1");
        String birthDate2 = (String)params.get("birthDate2");
        String retirementTime1 = (String)params.get("retirementTime1");
        String retirementTime2 = (String)params.get("retirementTime2");
        String workTime1 = (String)params.get("workTime1");
        String workTime2 = (String)params.get("workTime2");

        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",0).
                        like(StringUtils.isNotBlank(myStatus),"my_status", myStatus).
                        like(StringUtils.isNotBlank(name),"name", name).
                        eq(StringUtils.isNotBlank(idNo),"id_no",idNo).
                        eq(StringUtils.isNotBlank(personType),"person_type",personType).
                        like(StringUtils.isNotBlank(originalTroops),"original_troops", originalTroops).
                        like(StringUtils.isNotBlank(retirementStatus),"retirement_status", retirementStatus).
                        like(StringUtils.isNotBlank(duties),"duties",duties).
                        like(StringUtils.isNotBlank(county),"county", county).
                        like(StringUtils.isNotBlank(street),"street", street).
                        ge(StringUtils.isNotBlank(birthDate1),"birth_date", birthDate1).
                        le(StringUtils.isNotBlank(birthDate2),"birth_date", birthDate2).
                        ge(StringUtils.isNotBlank(retirementTime1),"retirement_time", retirementTime1).
                        le(StringUtils.isNotBlank(retirementTime2),"retirement_time", retirementTime2).
                        ge(StringUtils.isNotBlank(workTime1),"work_time", workTime1).
                        le(StringUtils.isNotBlank(workTime2),"work_time", workTime2)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils searchPage(Map<String, Object> params) {
        String myStatus = (String)params.get("myStatus");
        String name = (String)params.get("name");
        String idNo = (String)params.get("idNo");
        String personType = (String)params.get("personType");
        String originalTroops = (String)params.get("originalTroops");
        String retirementStatus = (String)params.get("retirementStatus");
        String duties = (String)params.get("duties");
        String county = (String)params.get("county");
        String street = (String)params.get("street");
        String birthDate1 = (String)params.get("birthDate1");
        String birthDate2 = (String)params.get("birthDate2");
        String retirementTime1 = (String)params.get("retirementTime1");
        String retirementTime2 = (String)params.get("retirementTime2");
        String workTime1 = (String)params.get("workTime1");
        String workTime2 = (String)params.get("workTime2");

        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",0).
                        like(StringUtils.isNotBlank(myStatus),"my_status", myStatus).
                        like(StringUtils.isNotBlank(name),"name", name).
                        eq(StringUtils.isNotBlank(idNo),"id_no",idNo).
                        eq(StringUtils.isNotBlank(personType),"person_type",personType).
                        like(StringUtils.isNotBlank(originalTroops),"original_troops", originalTroops).
                        like(StringUtils.isNotBlank(retirementStatus),"retirement_status", retirementStatus).
                        like(StringUtils.isNotBlank(duties),"duties",duties).
                        like(StringUtils.isNotBlank(county),"county", county).
                        like(StringUtils.isNotBlank(street),"street", street).
                        ge(StringUtils.isNotBlank(birthDate1),"birth_date", birthDate1).
                        le(StringUtils.isNotBlank(birthDate2),"birth_date", birthDate2).
                        ge(StringUtils.isNotBlank(retirementTime1),"retirement_time", retirementTime1).
                        le(StringUtils.isNotBlank(retirementTime2),"retirement_time", retirementTime2).
                        ge(StringUtils.isNotBlank(workTime1),"work_time", workTime1).
                        le(StringUtils.isNotBlank(workTime2),"work_time", workTime2)
        );

        return new PageUtils(page);
    }



    @Override
    public List<HonorableEmployeesEntity> getName(String query) {
        return baseMapper.getName(query);
    }

    @Override
    public List<HonorableEmployeesEntity> listExcel(Map<String, Object> params) {
        List<HonorableEmployeesEntity> list = baseMapper.listExcel();
        List<HonorableEmployeesEntity> listAll = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            HonorableEmployeesEntity honorableEmployeesEntity = new HonorableEmployeesEntity();

            honorableEmployeesEntity.setName(list.get(i).getName());//姓名
            honorableEmployeesEntity.setSex(list.get(i).getSex());//性别
            honorableEmployeesEntity.setDuties(list.get(i).getDuties());//职务
            honorableEmployeesEntity.setWorkTime(list.get(i).getWorkTime());//参加工作时间
            honorableEmployeesEntity.setRetirementTime(list.get(i).getRetirementTime());//离休时间
            //基本离休费合计
            Double basicSalary = list.get(i).getBasicSalary();//基础工资
            Double senioritySalary = list.get(i).getSenioritySalary();//工龄工资
            Double seniorityAllowance = list.get(i).getSeniorityAllowance();//教护龄津贴
            Double specialTeacherSubsidy = list.get(i).getSpecialTeacherSubsidy();//特级老师补贴
            Double raiseWages = list.get(i).getRaiseWages();//提高工资10%
            Double serviceAllowance = list.get(i).getServiceAllowance();//军队服务津贴
            Double gradeSalary = list.get(i).getGradeSalary();//岗位工资(职务、级别标准工资)
            Double levelSalary = list.get(i).getLevelSalary();//级别工资
            Double bonusAllowance = list.get(i).getBonusAllowance();//奖金津贴

            Double sumA = basicSalary + senioritySalary + seniorityAllowance + specialTeacherSubsidy + raiseWages + serviceAllowance + gradeSalary + levelSalary + bonusAllowance;
            honorableEmployeesEntity.setResultA(sumA);
            //历次增加离休费合计
            Double retirementPayEightynine = list.get(i).getRetirementPayEightynine();//1989年10月增加退休费
            Double retirementPayNinetyfive = list.get(i).getRetirementPayNinetyfive();//1995年10月增加退休费
            Double retirementPayNinetynisec = list.get(i).getRetirementPayNinetynisec();//1999年10月增加退休费
            Double retirementPayThisec = list.get(i).getRetirementPayThisec();//2003年10月增加退休费
            Double retirementPayFirst = list.get(i).getRetirementPayFirst();//1993年10月第一步增加退休费
            Double retirementPayNinetyseven = list.get(i).getRetirementPayNinetyseven();//1997年7月增加退休费
            Double retirementPayOne = list.get(i).getRetirementPayOne();//2001年1月增加退休费
            Double retirementPayThird = list.get(i).getRetirementPayThird();//2003年7月增加退休费
            Double retirementPayFourty = list.get(i).getRetirementPayFourty();//2014年10月增加退休费
            Double retirementPayNinetytwo = list.get(i).getRetirementPayNinetytwo();//1992年3月提高退休费10％
            Double retirementPayNinetysesec = list.get(i).getRetirementPayNinetysesec();//1997年10月增加退休费
            Double retirementPayOnsec = list.get(i).getRetirementPayOnsec();//2001年10月增加退休费
            Double retirementPayFive = list.get(i).getRetirementPayFive();//2005年10月增加退休费
            Double retirementPaySecond = list.get(i).getRetirementPaySecond();//1993年10月第二步增加退休费
            Double retirementPayNinetynine = list.get(i).getRetirementPayNinetynine();//1999年7月增加退休费
            Double retirementPayOnenew = list.get(i).getRetirementPayOnenew();//2001年10月新增退休费
            Double retirementPaySix = list.get(i).getRetirementPaySix();//2006年7月增加退休费
            Double retirementPaySixteen = list.get(i).getRetirementPaySixteen();//2016年7月增加离休费
            Double retirementPayEighteen = list.get(i).getRetirementPayEighteen();//2018年7月增加离休费
            Double retirementPayTwentyten = list.get(i).getRetirementPayTwentyten();//2021年10月增加离休费

            Double sumB = retirementPayEightynine + retirementPayNinetyfive + retirementPayNinetynisec + retirementPayThisec + retirementPayFirst + retirementPayNinetyseven +
                    retirementPayOne + retirementPayThird + retirementPayFourty + retirementPayNinetytwo + retirementPayNinetysesec + retirementPayOnsec + retirementPayFive +
                    retirementPaySecond + retirementPayNinetynine + retirementPayOnenew + retirementPaySix + retirementPaySixteen + retirementPayEighteen + retirementPayTwentyten;
            honorableEmployeesEntity.setResultB(sumB);

            //离休费合计
            Double sumG = sumA + sumB;
            honorableEmployeesEntity.setResultG(sumG);

            //应发生活补贴合计
            if(list.get(i).getWorkTime() != null && !list.get(i).getWorkTime().equals(""))
            {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String time =format.format(list.get(i).getWorkTime());

                int nian = Integer.valueOf(time.substring(0,4));
                int month = Integer.valueOf(time.substring(5,7));
                int day = Integer.valueOf(time.substring(8,10));

                Double shbthj =0.0;

                if(nian == 1937 && month > 7 )
                {
                    shbthj = sumG*3.5;
                }else if(nian == 1937 && month == 7 && day >= 7)
                {
                    shbthj = sumG*3.5;
                }else if(nian > 1937 && nian <1943)
                {
                    shbthj = sumG*3.5;
                }else if(nian >= 1943 && nian <1945)
                {
                    shbthj = sumG*3;
                }else if(nian == 1945 && month < 9)
                {
                    shbthj = sumG*3;
                }else if(nian == 1945 && month == 9 && day <= 2)
                {
                    shbthj = sumG*3;
                }else if(nian == 1945 && month == 9 && day >=3 )
                {
                    shbthj = sumG*2;
                }else if(nian == 1945 && month > 9)
                {
                    shbthj = sumG*2;
                }else if(nian > 1945 && nian < 1949)
                {
                    shbthj = sumG*2;
                }else if(nian == 1949 && month < 10)
                {
                    shbthj = sumG*2;
                }
                honorableEmployeesEntity.setResultS(shbthj);
            }

            honorableEmployeesEntity.setStreet(list.get(i).getStreet());
            listAll.add(honorableEmployeesEntity);
        }


        return listAll;
    }

    @Override
    public void removeByState(Long[] ids) {
        baseMapper.removeByState(ids);
    }

    @Override
    public List<HonorableEmployeesEntity> queryErrorPage(Map<String, Object> params) {
        List<HonorableEmployeesEntity> list = new ArrayList<>();
        List<HonorableEmployeesEntity> listAll = baseMapper.getAllList();

        for (int i = 0; i <listAll.size() ; i++) {
        HonorableEmployeesEntity honorableEmployeesEntity = new HonorableEmployeesEntity();
        HonorableEmployeesEntity hEmployees = listAll.get(i);
        honorableEmployeesEntity.setPersonalCode(hEmployees.getPersonalCode());//个人编码
        honorableEmployeesEntity.setName(hEmployees.getName());//姓名
        honorableEmployeesEntity.setDeath("错误");
        //基本离休费合计
        Double basicSalary = hEmployees.getBasicSalary();//基础工资
        Double senioritySalary = hEmployees.getSenioritySalary();//工龄工资
        Double seniorityAllowance = hEmployees.getSeniorityAllowance();//教护龄津贴
        Double specialTeacherSubsidy = hEmployees.getSpecialTeacherSubsidy();//特级老师补贴
        Double raiseWages = hEmployees.getRaiseWages();//提高工资10%
        Double serviceAllowance = hEmployees.getServiceAllowance();//军队服务津贴
        Double gradeSalary = hEmployees.getGradeSalary();//岗位工资(职务、级别标准工资)
        Double levelSalary = hEmployees.getLevelSalary();//级别工资
        Double bonusAllowance = hEmployees.getBonusAllowance();//奖金津贴

        Double sumA = basicSalary + senioritySalary + seniorityAllowance + specialTeacherSubsidy + raiseWages + serviceAllowance + gradeSalary + levelSalary + bonusAllowance;
        honorableEmployeesEntity.setResultA(sumA);

        //历次增加离休费合计
        Double retirementPayEightynine = hEmployees.getRetirementPayEightynine();//1989年10月增加退休费
        Double retirementPayNinetyfive = hEmployees.getRetirementPayNinetyfive();//1995年10月增加退休费
        Double retirementPayNinetynisec = hEmployees.getRetirementPayNinetynisec();//1999年10月增加退休费
        Double retirementPayThisec = hEmployees.getRetirementPayThisec();//2003年10月增加退休费
        Double retirementPayFirst = hEmployees.getRetirementPayFirst();//1993年10月第一步增加退休费
        Double retirementPayNinetyseven = hEmployees.getRetirementPayNinetyseven();//1997年7月增加退休费
        Double retirementPayOne = hEmployees.getRetirementPayOne();//2001年1月增加退休费
        Double retirementPayThird = hEmployees.getRetirementPayThird();//2003年7月增加退休费
        Double retirementPayFourty = hEmployees.getRetirementPayFourty();//2014年10月增加退休费
        Double retirementPayNinetytwo = hEmployees.getRetirementPayNinetytwo();//1992年3月提高退休费10％
        Double retirementPayNinetysesec = hEmployees.getRetirementPayNinetysesec();//1997年10月增加退休费
        Double retirementPayOnsec = hEmployees.getRetirementPayOnsec();//2001年10月增加退休费
        Double retirementPayFive = hEmployees.getRetirementPayFive();//2005年10月增加退休费
        Double retirementPaySecond = hEmployees.getRetirementPaySecond();//1993年10月第二步增加退休费
        Double retirementPayNinetynine = hEmployees.getRetirementPayNinetynine();//1999年7月增加退休费
        Double retirementPayOnenew = hEmployees.getRetirementPayOnenew();//2001年10月新增退休费
        Double retirementPaySix = hEmployees.getRetirementPaySix();//2006年7月增加退休费
        Double retirementPaySixteen = hEmployees.getRetirementPaySixteen();//2016年7月增加离休费
        Double retirementPayEighteen = hEmployees.getRetirementPayEighteen();//2018年7月增加离休费
        Double retirementPayTwentyten = hEmployees.getRetirementPayTwentyten();//2021年10月增加离休费

        Double sumB = retirementPayEightynine + retirementPayNinetyfive + retirementPayNinetynisec + retirementPayThisec + retirementPayFirst + retirementPayNinetyseven +
                retirementPayOne + retirementPayThird + retirementPayFourty + retirementPayNinetytwo + retirementPayNinetysesec + retirementPayOnsec + retirementPayFive +
                retirementPaySecond + retirementPayNinetynine + retirementPayOnenew + retirementPaySix + retirementPaySixteen + retirementPayEighteen + retirementPayTwentyten;
        honorableEmployeesEntity.setResultB(sumB);

        //离休补贴合计
        Double growthMechanismSubsidy = hEmployees.getGrowthMechanismSubsidy();//2012年7月增加离休补贴
        Double retirementPayNineteen = hEmployees.getRetirementPayNineteen();//2019年1月增加离休补贴
        Double retirementPayTwentyseven = hEmployees.getRetirementPayTwentyseven();//2021年7月增加离休补贴

        Double sumC = growthMechanismSubsidy + retirementPayNineteen + retirementPayTwentyseven;
        honorableEmployeesEntity.setResultC(sumC);

        //其它合计
        Double pensionMedicalSubsidy = hEmployees.getPensionMedicalSubsidy();//养老医疗补贴
        Double nursingFee = hEmployees.getNursingFee();//护理费（自雇费）
        Double carAllowance = hEmployees.getCarAllowance();//用车包干补贴
        Double honorAllowance = hEmployees.getHonorAllowance();//荣誉津贴
        Double serviceSubsidy = hEmployees.getServiceSubsidy();//物业服务补贴

        Double sumG = pensionMedicalSubsidy + nursingFee + carAllowance + honorAllowance + serviceSubsidy;
        honorableEmployeesEntity.setResultG(sumG);

        //本年度月退休金总额
        Double sumH = sumA + sumB + sumC + sumG;
        honorableEmployeesEntity.setResultH(sumH);

        honorableEmployeesEntity.setTotalMonthlyPension(hEmployees.getTotalMonthlyPension());//原单位发放退休金

        //错误差额
        Double ydwtxj = hEmployees.getTotalMonthlyPension();
        Double sumI = ydwtxj - sumH;
        honorableEmployeesEntity.setResultI(sumI);

        if(sumI >0 || sumI <0)
            {
                list.add(honorableEmployeesEntity);
            }

        }
        return list;
    }

    @Override
    public PageUtils queryRepeatPage(Map<String, Object> params) {
        IPage<HonorableEmployeesEntity> page = null;
        List<String> list = baseMapper.queryRepeatList();
        if(list.size() >0)
        {
            page = this.page(
                    new Query<HonorableEmployeesEntity>().getPage(params),
                    new QueryWrapper<HonorableEmployeesEntity>().
                            eq("state",0).in("personal_code",list).orderByAsc("personal_code")
            );
        }else
        {
            page = this.page(
                    new Query<HonorableEmployeesEntity>().getPage(params),
                    new QueryWrapper<HonorableEmployeesEntity>().
                            eq("state",3)
            );
        }


        return new PageUtils(page);
    }

    @Override
    public PageUtils queryDeathPage(Map<String, Object> params) {
        String deathTime1 = (String)params.get("deathTime1");
        String deathTime2 = (String)params.get("deathTime2");
        String logoutDate1 = (String)params.get("logoutDate1");
        String logoutDate2 = (String)params.get("logoutDate2");
        String county = (String)params.get("county");
        String street = (String)params.get("street");

        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",0).eq("my_status","死亡").
                        eq(StringUtils.isNotBlank(county),"county", county).
                        eq(StringUtils.isNotBlank(street),"street", street).
                        ge(StringUtils.isNotBlank(deathTime1),"death_time", deathTime1).
                        le(StringUtils.isNotBlank(deathTime2),"death_time", deathTime2).
                        ge(StringUtils.isNotBlank(logoutDate1),"logout_date", logoutDate1).
                        le(StringUtils.isNotBlank(logoutDate2),"logout_date", logoutDate2)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils searchDeathPage(Map<String, Object> params) {
        String deathTime1 = (String)params.get("deathTime1");
        String deathTime2 = (String)params.get("deathTime2");
        String logoutDate1 = (String)params.get("logoutDate1");
        String logoutDate2 = (String)params.get("logoutDate2");
        String county = (String)params.get("county");
        String street = (String)params.get("street");

        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",0).eq("my_status","死亡").
                        eq(StringUtils.isNotBlank(county),"county", county).
                        eq(StringUtils.isNotBlank(street),"street", street).
                        ge(StringUtils.isNotBlank(deathTime1),"death_time", deathTime1).
                        le(StringUtils.isNotBlank(deathTime2),"death_time", deathTime2).
                        ge(StringUtils.isNotBlank(logoutDate1),"logout_date", logoutDate1).
                        le(StringUtils.isNotBlank(logoutDate2),"logout_date", logoutDate2)
        );

        return new PageUtils(page);
    }



    @Override
    public List<HonorableEmployeesEntity> listDeathExcel(Map<String, Object> params) {
        String deathTime1 = (String)params.get("deathTime1");
        String deathTime2 = (String)params.get("deathTime2");
        String logoutDate1 = (String)params.get("logoutDate1");
        String logoutDate2 = (String)params.get("logoutDate2");
        String county = (String)params.get("county");
        String street = (String)params.get("street");

        QueryWrapper<RetiredEmployeesEntity>  queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("state",0);
        queryWrapper.eq("my_status","死亡");
        queryWrapper.eq(StringUtils.isNotBlank(county),"county", county);
        queryWrapper.eq(StringUtils.isNotBlank(street),"street", street);
        queryWrapper.ge(StringUtils.isNotBlank(deathTime1),"death_time", deathTime1);
        queryWrapper.le(StringUtils.isNotBlank(deathTime2),"death_time", deathTime2);
        queryWrapper.ge(StringUtils.isNotBlank(logoutDate1),"logout_date", logoutDate1);
        queryWrapper.le(StringUtils.isNotBlank(logoutDate2),"logout_date", logoutDate2);

        return baseMapper.listDeathExcel(queryWrapper);
    }

    @Override
    public PageUtils queryDeletePage(Map<String, Object> params) {
        String personalCode = (String)params.get("personalCode");
        String name = (String)params.get("name");
        String idNo = (String)params.get("idNo");


        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",1).
                        eq(StringUtils.isNotBlank(personalCode),"personal_code", personalCode).
                        eq(StringUtils.isNotBlank(name),"name", name).
                        eq(StringUtils.isNotBlank(idNo),"id_no", idNo)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils searchDeletePage(Map<String, Object> params) {
        String personalCode = (String)params.get("personalCode");
        String name = (String)params.get("name");
        String idNo = (String)params.get("idNo");


        IPage<HonorableEmployeesEntity> page = this.page(
                new Query<HonorableEmployeesEntity>().getPage(params),
                new QueryWrapper<HonorableEmployeesEntity>().
                        eq("state",1).
                        eq(StringUtils.isNotBlank(personalCode),"personal_code", personalCode).
                        eq(StringUtils.isNotBlank(name),"name", name).
                        eq(StringUtils.isNotBlank(idNo),"id_no", idNo)
        );

        return new PageUtils(page);
    }

    @Override
    public void recoveryById(Long[] ids) {
        baseMapper.recoveryById(ids);
    }



}
